package base;

import java.util.*;

/**
 * This class is used to deal with orders
 *
 * @author XFishalways
 */
public class Order {

    /**
     * Products assigned to an order
     */
    private Vector<Product> item;
    private String orderNo;

    /**
     * Sets the collection of {@link Product} to empty.
     */
    public Order() {

        item = new Vector<Product>();
    }

    /**
     * Adds an item to this order
     *
     * @param code
     */
    public void addItem(Product code) {

        item.add(code);
    }

    /**
     * Removes a {@link Product} object from the order
     *
     * @param code the {@link Product} object.
     */
    public boolean delItem(Product code) {

        return item.removeElement(code);
    }

    /**
     * Clear current items from this order
     */
    public void clearItem() {

        item.clear();
    }

    /**
     * @param code 
     * @return
     */
    public int getNumberOfItem(String code) {

        return item.size();
    }

    /**
     * Returns an iterator on sold product in this order
     *
     * Returns an {@link Iterator}
     */
    public Iterator<Product> getItemsIterator() {

        return item.iterator();
    }

    /**
     * Returns the {@link Product} object with the specified
     * <code>code</code>.
     *
     * @param code the code of an item.
     * @return  The {@link Product} object with the specified code.
     *          Returns <code>null</code> if the object with the code is not found.
     */
    public Product getItem(String code) {
        for (Iterator<Product> i = getItemsIterator(); i.hasNext(); ) {
            Product product = i.next();
            if (product.getCode().equals(code)) {
                return product;
            }
        }
        return null;
    }

    /**
     * Returns the number of this order
     *
     * @return Returns the number of this order
     */
    public String getOrderNo() {
        return orderNo;
    }
    /**
     * Returns whether the item is empty
     */
    public boolean empty()
    {
        return item.isEmpty();
    }
    /**
     * Output the information of this order
     */
    public void showOrder() {
        for(Product P:item)
            P.showProduct();
    }
}